class Memento {
    private state: string;
    constructor(state: string) {
        this.state = state;
    }
    public getState(): string {
        return this.state;
    }
}

class Originator {
    private state: string = '';
    public setState(state: string): void {
        this.state = state;
    }
    public getState(): string {
        return this.state;
    }
    public saveStateToMemento(): Memento {
        return new Memento(this.state);
    }
    public getStateFromMemento(memento: Memento) {
        this.state = memento.getState();
    }
}

class CareTaker {
    private mementos: Memento[] = [];
    public add(memento: Memento): void {
        this.mementos.push(memento);
    }
    public get(index: number): Memento {
        return this.mementos[index];
    }
}

function main() {
    const originator: Originator = new Originator();
    const careTaker: CareTaker = new CareTaker();
    originator.setState("State1");
    originator.setState("State2");
    careTaker.add(originator.saveStateToMemento());
    originator.setState("State3");
    careTaker.add(originator.saveStateToMemento());
    originator.setState("State4");
    console.log('Current state: ' + originator.getState());
    originator.getStateFromMemento(careTaker.get(0));
    console.log('First saved state: ' + originator.getState());
    originator.getStateFromMemento(careTaker.get(1));
    console.log('Second saved state: ' + originator.getState());
}

main();
